package com.jpyl.jpMedicalTreatment.view.fragment.common;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;

import com.forxindu.kit.box.PreferenceKit;
import com.forxindu.kit.box.ViewKit;
import com.jpyl.jpMedicalTreatment.MedicalApplication;
import com.jpyl.jpMedicalTreatment.R;
import com.jpyl.jpMedicalTreatment.common.MedicalSettings;
import com.jpyl.jpMedicalTreatment.model.ResponseModel;
import com.jpyl.jpMedicalTreatment.model.RxSpf_UserModel;
import com.jpyl.jpMedicalTreatment.network.Const;
import com.jpyl.jpMedicalTreatment.network.Network;
import com.jpyl.jpMedicalTreatment.network.api.ApiError;
import com.jpyl.jpMedicalTreatment.network.api.RequestApi;
import com.jpyl.jpMedicalTreatment.view.activity.HomeActivity;
import com.jpyl.jpMedicalTreatment.view.activity.LoginActivity;
import com.jpyl.jpMedicalTreatment.view.activity.self.DeliveryActivity;
import com.jpyl.jpMedicalTreatment.view.activity.self.FeedbackActivity;
import com.jpyl.jpMedicalTreatment.view.fragment.BaseFragment;
import com.jpyl.jpMedicalTreatment.view.widget.LogOutDialog;

import butterknife.OnClick;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Func1;
import rx.schedulers.Schedulers;

/**
 * 总代理-我的
 * Created by 521546564465 on 2018/11/5.
 */

@SuppressLint("ValidFragment")
public class GeneralAgentSelfFragment extends BaseFragment {

    RxSpf_UserModel spfUserModel;

    @SuppressLint("ValidFragment")
    public GeneralAgentSelfFragment(Context context) {
        // TODO Auto-generated constructor stub
        super(context);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_general_agent_mine;
    }

    @Override
    protected void afterCreate(Bundle savedInstanceState) {
        spfUserModel = RxSpf_UserModel.create(getContext());
    }

    @OnClick({R.id.rela_generalAgent_feekBack, R.id.rela_generalAgent_toBeShipped, R.id
            .rela_generalAgent_shipped, R.id.rela_generalAgent_signOut})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            //反馈、申诉、举报
            case R.id.rela_generalAgent_feekBack:
                startActivity(new Intent(getActivity(), FeedbackActivity.class));
                break;
            //待发货
            case R.id.rela_generalAgent_toBeShipped:
                startActivity(new Intent(getActivity(), DeliveryActivity.class).putExtra
                        ("flag", Const.TOSHIPPED));
                break;

            //已发货
            case R.id.rela_generalAgent_shipped:
                startActivity(new Intent(getActivity(), DeliveryActivity.class).putExtra
                        ("flag", Const.SHIPPED));
                break;
            //退出
            case R.id.rela_generalAgent_signOut:
                showLogOutDialog();
                break;
        }
    }
    public void showLogOutDialog() {
        LogOutDialog.Builder builder = new LogOutDialog.Builder(getActivity());
        LogOutDialog dialog = builder.setMessage("退出登录？").setTitle("").setPositiveButton("退出", new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {
                logout();
                dialog.dismiss();
            }
        }).setNegativeButton("取消", new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {
                // TODO Auto-generated method stub
                dialog.dismiss();
            }
        }).create();
        dialog.setCancelable(false);
        dialog.show();
    }

    final RequestApi publicApi = Network.getRequestApi();
    //退出登陆
    void logout() {
        publicApi.jpylLogout(spfUserModel.loginname().get()).map(new Func1<ResponseModel<String>,
                String>() {
            @Override
            public String call(ResponseModel<String> responseModel) {
                if (responseModel.getCode() != 0) {
                    String msg = responseModel.getMsg();
                    throw new ApiError(responseModel.getCode(), msg);
                }
                return responseModel.getMsg();
            }
        })
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Subscriber<String>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {
                        e.printStackTrace();
                        if (e instanceof ApiError) {
                            ApiError error = (ApiError) e;
                            ViewKit.showToast(getContext(), error.getMsg() + "");
                        }
                    }

                    @Override
                    public void onNext(String msg) {
                        ViewKit.showToast(getContext(), msg + "");
                        if (msg.equals("登出成功")) {
                            Intent intent = new Intent(getActivity(), LoginActivity.class);
                            startActivity(intent);
                            HomeActivity.activity.finish();
                            PreferenceKit.setSharedPreference(MedicalApplication.applicationContext, MedicalSettings.MEMBER_COOKIE, "");
                        }
                    }
                });
    }
}
